package com.example.mymusicdisplay.view;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.databinding.FragmentMainBinding;
import com.example.mymusicdisplay.view.adapter.ViewPagerAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainFragment extends Fragment {
    private static final String TAG = "MainFragment";
    private FragmentMainBinding mMainBinding;
    private List<Fragment> mFragmentList;
    private ViewPagerAdapter mAdapter;
    private FindMusicFragment mFindMusicFragment;
    private UserCenterFragment mUserCenterFragment;

    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: ");
        mMainBinding = DataBindingUtil
            .inflate(LayoutInflater.from(requireContext()), R.layout.fragment_main, container, false);
        ViewCompat.setOnApplyWindowInsetsListener(mMainBinding.mainTitle, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                layoutParams.topMargin = insets.getSystemWindowInsetTop();
                return insets;
            }
        });
        return mMainBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated: ");
        initView();
        setSelectListener();
    }


    private void initView() {

        mFragmentList = new ArrayList<>();

        mFindMusicFragment = new FindMusicFragment(requireContext());
        mUserCenterFragment = new UserCenterFragment();

        mFragmentList.add(mFindMusicFragment);
        mFragmentList.add(mUserCenterFragment);

        mAdapter = new ViewPagerAdapter(getChildFragmentManager(), mFragmentList, requireContext());

        mMainBinding.viewPager.setAdapter(mAdapter);

        mMainBinding.tabLayout.setupWithViewPager(mMainBinding.viewPager);

    }

    private void setSelectListener() {
        Log.d(TAG, "setSelectListener: ");
        for (int i = 0; i < mMainBinding.tabLayout.getTabCount(); i++) {
            TabLayout.Tab tabLayoutTabAt = mMainBinding.tabLayout.getTabAt(i);
            tabLayoutTabAt.setCustomView(mAdapter.getCustomView(i));
            if (i == 0) {
                tabLayoutTabAt.getCustomView().findViewById(R.id.item_image).setSelected(true);
                tabLayoutTabAt.getCustomView().findViewById(R.id.item_text).setSelected(true);
            }
        }

        mMainBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.item_image).setSelected(true);
                tab.getCustomView().findViewById(R.id.item_text).setSelected(true);
                mMainBinding.viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.item_image).setSelected(false);
                tab.getCustomView().findViewById(R.id.item_text).setSelected(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //重新选中
            }
        });
    }
}